package hn.sigit.model.hnd.special;

import hn.sigit.model.hnd.administrative.HND_Transaction;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;


@Embeddable
public class HND_Lock {
	private HND_Transaction lockingTransaction;
	private Integer lockLevel;
	
	
	@ManyToOne(targetEntity=HND_Transaction.class, optional=true, fetch=FetchType.LAZY)	
	@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.LOCK})	
	@JoinColumns({ @JoinColumn(name="TransactionID", referencedColumnName="id") })	
	@org.hibernate.annotations.LazyToOne(value=org.hibernate.annotations.LazyToOneOption.NO_PROXY)
	public HND_Transaction getLockingTransaction() {
		return lockingTransaction;
	}
	public void setLockingTransaction(HND_Transaction lockingTransaction) {
		this.lockingTransaction = lockingTransaction;
	}
	
	@Column(name="LockLevel", nullable=true)
	public Integer getLockLevel() {
		return lockLevel;
	}
	public void setLockLevel(Integer lockLevel) {
		this.lockLevel = lockLevel;
	}
	
	
	@Transient
	public boolean isLocked() {
		return lockingTransaction != null;
	}
	@Transient
	public void unlock() {
		lockingTransaction = null;
		lockLevel = null;
	}
}
